/*=============================================================================

  Library: HMPBundleFrame

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/

#include "BundleArchiveSQL_p.h"

#include "BundleException.h"
#include "BundleStorageSQL_p.h"
#include "BundleDatabaseException.h"

#include <QStringList>
#include <QFile>


//----------------------------------------------------------------------------
PluginArchiveSQL::PluginArchiveSQL(PluginStorageSQL* pluginStorage,
                                         const QUrl& pluginLocation, const QString& localPluginPath,
                                         int pluginId, int startLevel, const QDateTime& lastModified,
                                         int autostartSetting)
  : key(-1), autostartSetting(autostartSetting), id(pluginId), generation(0)
  , startLevel(startLevel), lastModified(lastModified), location(pluginLocation)
  , localPluginPath(localPluginPath), storage(pluginStorage)
{
}

//----------------------------------------------------------------------------
PluginArchiveSQL::PluginArchiveSQL(QSharedPointer<PluginArchiveSQL> old, int generation,
                                         const QUrl &pluginLocation, const QString &localPluginPath)
  : key(-1), autostartSetting(old->autostartSetting), id(old->id), generation(generation)
  , startLevel(0), location(pluginLocation), localPluginPath(localPluginPath)
  , storage(old->storage)
{
}

void PluginArchiveSQL::readManifest(const QByteArray& manifestResource)
{
  QByteArray manifestRes = manifestResource.isNull() ? this->getPluginResource("HMP-RES/MANIFEST.MF")
                                                  : manifestResource;
  if (manifestRes.isEmpty())
  {
    throw BundleException(QString("Bundle has no MANIFEST.MF resource, location=") + localPluginPath);
  }

  manifest.read(manifestRes);
}

//----------------------------------------------------------------------------
QString PluginArchiveSQL::getAttribute(const QString& key) const
{
  return manifest.getAttribute(key);
}

//----------------------------------------------------------------------------
QHash<QString,QString> PluginArchiveSQL::getUnlocalizedAttributes() const
{
  return manifest.getMainAttributes();
}

int PluginArchiveSQL::getPluginGeneration() const
{
  return generation;
}

//----------------------------------------------------------------------------
int PluginArchiveSQL::getPluginId() const
{
  return id;
}

//----------------------------------------------------------------------------
QUrl PluginArchiveSQL::getPluginLocation() const
{
  return location;
}

//----------------------------------------------------------------------------
QString PluginArchiveSQL::getLibLocation() const
{
  return localPluginPath;
}

//----------------------------------------------------------------------------
QByteArray PluginArchiveSQL::getPluginResource(const QString& component) const
{
  try
  {
    return storage->getPluginResource(key, component);
  }
  catch (const PluginDatabaseException& exc)
  {
    qDebug() << QString("Getting plugin resource %1 failed:").arg(component) << exc;
    return QByteArray();
  }
}

//----------------------------------------------------------------------------
QStringList PluginArchiveSQL::findResourcesPath(const QString& path) const
{
  try
  {
    return storage->findResourcesPath(key, path);
  }
  catch (const PluginDatabaseException& exc)
  {
    qDebug() << QString("Getting plugin resource paths for %1 failed:").arg(path) << exc;
  }
  return QStringList();
}

//----------------------------------------------------------------------------
int PluginArchiveSQL::getStartLevel() const
{
  return startLevel;
}

//----------------------------------------------------------------------------
void PluginArchiveSQL::setStartLevel(int level)
{
  if (startLevel != level)
  {
    startLevel = level;
    //storage->setStartLevel(key, level);
  }
}

//----------------------------------------------------------------------------
QDateTime PluginArchiveSQL::getLastModified() const
{
  return lastModified;
}

//----------------------------------------------------------------------------
void PluginArchiveSQL::setLastModified(const QDateTime& dateTime)
{
  lastModified = dateTime;
 // storage->setLastModified(key, dateTime);
}

//----------------------------------------------------------------------------
int PluginArchiveSQL::getAutostartSetting() const
{
  return autostartSetting;
}

//----------------------------------------------------------------------------
void PluginArchiveSQL::setAutostartSetting(int setting)
{
  if (autostartSetting != setting)
  {
    autostartSetting = setting;
    storage->setAutostartSetting(key, setting);
  }
}

//----------------------------------------------------------------------------
void PluginArchiveSQL::purge()
{
  storage->removeArchive(this);
}

//----------------------------------------------------------------------------
void PluginArchiveSQL::close()
{

}
